package cn.qdhlm.collectservice.common.util;

import org.apache.commons.codec.binary.Base64;

/**
 * @Name: CodeUtil
 * @Description: CodeUtil
 * @Author: jiangcs
 **/
public class CodeUtil {


    /**
     * short to hex string： 2 byte
     *
     * @param s
     * @return string
     */
    public static String shortToHexString(short s) {
        int v = s & 0xFFFF;
        return Integer.toHexString(v);
    }
    /**
     * hex string to base64 string
     *
     * @param hexStr
     * @return
     */
    public static String hexStringToBase64String(String hexStr) {
        return byteArrayToBase64String(hexStringToByteArray(hexStr));
    }

    /**
     * int to hex string： 4 byte
     *
     * @param i
     * @return string
     */
    public static String intToHexString(int i) {
        return Integer.toHexString(i);
    }

    /**
     * base64 string to byte[]
     *
     * @param base64Str
     * @return byte[]
     */
    public static byte[] base64StringToByteArray(String base64Str) {
        return Base64.decodeBase64(base64Str);
    }

    /**
     * byte[] To base64 String
     *
     * @param byteArray
     * @return string
     */
    private static String byteArrayToBase64String(byte[] byteArray) {
        return Base64.encodeBase64String(byteArray);
    }

    /**
     * hex string to byte
     *
     * @param hexStr
     * @return
     */
    public static byte hexStringToByte(String hexStr, int index) {
        byte highDit = (byte) (Character.digit(hexStr.charAt(index), 16) & 0xFF);
        byte lowDit = (byte) (Character.digit(hexStr.charAt(index + 1), 16) & 0xFF);
        return (byte) (highDit << 4 | lowDit);
    }

    /**
     * hex string to byte[]
     *
     * @param hexStr
     * @return byte[]
     */
    public static byte[] hexStringToByteArray(String hexStr) {
        final byte[] byteArray = new byte[hexStr.length() >> 1];
        int index = 0;
        for (int i = 0; i < hexStr.length(); i++) {
            if (index > hexStr.length() - 1) {
                return byteArray;
            }
            byteArray[i] = hexStringToByte(hexStr, index);
            index += 2;
        }
        return byteArray;
    }

    /**
     * byte to hex string： 1 byte
     *
     * @param b
     * @return string
     */
    public static String byteToHexString(byte b) {
        int v = b & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            hv = "0" + hv;
        }
        return hv;
    }

    /**
     * hex string to ascii string
     *
     * @param hexStr
     * @return
     */
    public static String hexStringToAsciiString(String hexStr) {
        StringBuilder output = new StringBuilder();
        for (int i = 0, len = hexStr.length(); i < len; i += 2) {
            output.append((char) Integer.parseInt(hexStr.substring(i, i + 2), 16));
        }
        return output.toString();
    }

    /**
     * ascii string to hex string
     *
     * @param asciiStr
     * @return
     */
    public static String asciiStringToHexString(String asciiStr) {
        char[] chars = asciiStr.toCharArray();
        StringBuilder sbHex = new StringBuilder();
        for (char ch : chars) {
            sbHex.append(Integer.toHexString(ch));
        }
        return sbHex.toString();
    }
}
